#!/bin/sh
# PCP QA Test No. 1886
# Test function evaluation in pmseries queries
#
# Copyright (c) 2020 Red Hat.  All Rights Reserved.
#
seq=`basename $0`
echo "QA output created by $seq"
path=""

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check
. ./common.keys

# This test is not run if we dont have pmseries and a key server installed.
_check_series

_cleanup()
{
    [ -n "$key_server_port" ] && $keys_cli -p $key_server_port shutdown
    _restore_config $PCP_SYSCONF_DIR/pmseries
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter_source()
{
    sed \
	-e "s,$here,PATH,g" \
    #end
}

_sort_context()
{
    sed -e 's/    Context: //g' -e 's/, /\n/g' | \
    LC_COLLATE=POSIX sort | \
    $PCP_AWK_PROG 'BEGIN { printf "    Context: " } {
	if (count == 0) {printf("%s", $0)} else {printf(", %s", $0)};
	count++
    } END { print out }'
}

# real QA test starts here
key_server_port=`_find_free_port`
_save_config $PCP_SYSCONF_DIR/pmseries
$sudo rm -f $PCP_SYSCONF_DIR/pmseries/*

echo "Start test key server ..."
$key_server --port $key_server_port --save "" > $tmp.keys 2>&1 &
_check_key_server_ping $key_server_port
_check_key_server $key_server_port
echo

_check_key_server_version $key_server_port

args="-p $key_server_port -Z UTC"

echo "== Load metric data into this key server instance"
pmseries $args --load "{source.path: \"$here/archives/proc\"}" | _filter_source

echo;echo "== Verify rate() function for a singular metric"
pmseries $args 'kernel.all.pswitch[count:5]'
echo Compared to:
pmseries $args 'rate(kernel.all.pswitch[count:5])'
echo

echo;echo "== Verify min/max() functions for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
pmseries $args 'max(kernel.all.load[count:5])'
pmseries $args 'max_inst(kernel.all.load[count:5])'
pmseries $args 'max_sample(kernel.all.load[count:5])'
pmseries $args 'min(kernel.all.load[count:5])'
pmseries $args 'min_inst(kernel.all.load[count:5])'
pmseries $args 'min_sample(kernel.all.load[count:5])'

echo;echo "== Verify min/max() functions for a non-singular metric, only one instance"
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo Compared to max:
pmseries $args 'max(kernel.all.load{instance.name == "5 minute"}[count:5])'
pmseries $args 'max_inst(kernel.all.load{instance.name == "5 minute"}[count:5])'
pmseries $args 'max_sample(kernel.all.load{instance.name == "5 minute"}[count:5])'
echo Compared to min:
pmseries $args 'min(kernel.all.load{instance.name == "5 minute"}[count:5])'
pmseries $args 'min_inst(kernel.all.load{instance.name == "5 minute"}[count:5])'
pmseries $args 'min_sample(kernel.all.load{instance.name == "5 minute"}[count:5])'

echo;echo "== Verify min/max() functions for a singular metric"
pmseries $args 'kernel.all.uptime[count:10]'
echo Compared to max:
pmseries $args 'max(kernel.all.uptime[count:10])'
pmseries $args 'max_inst(kernel.all.uptime[count:10])'
pmseries $args 'max_sample(kernel.all.uptime[count:10])'
echo Compared to min:
pmseries $args 'min(kernel.all.uptime[count:10])'
pmseries $args 'min_inst(kernel.all.uptime[count:10])'
pmseries $args 'min_sample(kernel.all.uptime[count:10])'

echo;echo "== Verify rescale() functions for a singular metric"
pmseries $args 'kernel.all.uptime[count:2]'
echo Compared to rescale to sec:
pmseries $args 'rescale(kernel.all.uptime[count:2], "sec")'
echo Compared to rescale to min:
pmseries $args 'rescale(kernel.all.uptime[count:2], "min")'
echo Compared to rescale to min and then hour:
pmseries $args 'rescale(rescale(kernel.all.uptime[count:2], "min"), "hour")'

echo;echo "== Verify abs() functions for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
echo Compared to abs:
pmseries $args 'abs(kernel.all.load[count:5])'

echo;echo "== Verify abs() functions for a non-singular metric, only one instance"
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo Compared to abs:
pmseries $args 'abs(kernel.all.load{instance.name == "5 minute"}[count:5])'

echo;echo "== Verify log() functions for a singular metric"
pmseries $args 'kernel.all.pswitch[count:2]'
echo Compared to log:
pmseries $args 'log(kernel.all.pswitch[count:2],3)'
echo Compared to 'log(log(..,3), 2):'
pmseries $args 'log(log(kernel.all.pswitch[count:2],3),2)'

echo;echo "== Verify floor() functions for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
echo Compared to floor:
pmseries $args 'floor(kernel.all.load[count:5])'

echo;echo "== Verify round() functions for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
echo Compared to round:
pmseries $args 'round(kernel.all.load[count:5])'

echo;echo "== Verify round() functions for a singular metric"
pmseries $args 'kernel.all.uptime[count:5]'
echo Compared to round:
pmseries $args 'round(kernel.all.uptime[count:5])'

echo;echo "== Verify addition functions for a singular metric"
pmseries $args 'kernel.all.uptime[count:5]'
echo 'Compared to series + series:'
pmseries $args 'kernel.all.uptime[count:5] + kernel.all.uptime[count:5]'

echo;echo "== Verify addition functions for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
echo 'Compared to series + series:'
pmseries $args 'kernel.all.load[count:5] + kernel.all.load[count:5]'

echo;echo "== Verify addition functions for a non-singular metric, only one instance"
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo 'Compared to series + series:'
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5] + kernel.all.load{instance.name == "5 minute"}[count:5]'

echo;echo "== Verify subtraction functions for a singular metric"
pmseries $args 'kernel.all.uptime[count:5]'
echo 'Compared to series - series:'
pmseries $args 'kernel.all.uptime[count:5] - kernel.all.uptime[count:5]'

echo;echo "== Verify subtraction functions for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
echo 'Compared to series - series:'
pmseries $args 'kernel.all.load[count:5] - kernel.all.load[count:5]'

echo;echo "== Verify subtraction functions for a non-singular metric, only one instance"
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo 'Compared to series - series:'
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5] - kernel.all.load{instance.name == "5 minute"}[count:5]'

echo;echo "== Verify division functions for a singular metric"
pmseries $args 'kernel.all.uptime[count:5]'
echo 'Compared to series / series:'
pmseries $args 'kernel.all.uptime[count:5] / kernel.all.uptime[count:5]'

echo;echo "== Verify division functions for a non-singular metric"
pmseries $args 'kernel.all.load[count:5]'
echo 'Compared to series / series:'
pmseries $args 'kernel.all.load[count:5] / kernel.all.load[count:5]'

echo;echo "== Verify division functions for a non-singular metric, only one instance"
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo 'Compared to series / series:'
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5] / kernel.all.load{instance.name == "5 minute"}[count:5]'

echo;echo "== Verify sum/avg() functions for a non-singular metric"
echo Original with no function:
pmseries $args 'kernel.all.load[count:5]'
echo Compared to sum:
pmseries $args 'sum(kernel.all.load[count:5])'
pmseries $args 'sum_inst(kernel.all.load[count:5])'
pmseries $args 'sum_sample(kernel.all.load[count:5])'
echo Compared to avg:
pmseries $args 'avg(kernel.all.load[count:5])'
pmseries $args 'avg_inst(kernel.all.load[count:5])'
pmseries $args 'avg_sample(kernel.all.load[count:5])'

echo;echo "== Verify sum/avg() functions for a non-singular metric, only one instance"
echo Original with no function:
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo Compared to sum:
pmseries $args 'sum(kernel.all.load{instance.name == "5 minute"}[count:5])'
pmseries $args 'sum_inst(kernel.all.load{instance.name == "5 minute"}[count:5])'
pmseries $args 'sum_sample(kernel.all.load{instance.name == "5 minute"}[count:5])'
echo Compared to avg:
pmseries $args 'avg(kernel.all.load{instance.name == "5 minute"}[count:5])'
pmseries $args 'avg_inst(kernel.all.load{instance.name == "5 minute"}[count:5])'
pmseries $args 'avg_sample(kernel.all.load{instance.name == "5 minute"}[count:5])'

echo;echo "== Verify sum/avg() functions for a singular metric"
echo Original with no function:
pmseries $args 'rate(kernel.all.pswitch[count:10])'
echo Compared to sum:
pmseries $args 'sum(rate(kernel.all.pswitch[count:10]))'
pmseries $args 'sum_inst(rate(kernel.all.pswitch[count:10]))'
pmseries $args 'sum_sample(rate(kernel.all.pswitch[count:10]))'
echo Compared to avg:
pmseries $args 'avg(rate(kernel.all.pswitch[count:10]))'
pmseries $args 'avg_inst(rate(kernel.all.pswitch[count:10]))'
pmseries $args 'avg_sample(rate(kernel.all.pswitch[count:10]))'

echo;echo "== Verify stdev() functions for a non-singular metric"
echo Original with no function:
pmseries $args 'kernel.all.load[count:5]'
echo Compared to stdev:
pmseries $args 'stdev_inst(kernel.all.load[count:5])'
pmseries $args 'stdev_sample(kernel.all.load[count:5])'

echo;echo "== Verify stdev() functions for a non-singular metric, only one instance"
echo Original with no function:
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo Compared to stdev:
pmseries $args 'stdev_inst(kernel.all.load{instance.name == "5 minute"}[count:5])'
pmseries $args 'stdev_sample(kernel.all.load{instance.name == "5 minute"}[count:5])'

echo;echo "== Verify stdev() functions for a singular metric"
echo Original with no function:
pmseries $args 'kernel.all.uptime[count:10]'
echo Compared to stdev:
pmseries $args 'stdev_inst(kernel.all.uptime[count:10])'
pmseries $args 'stdev_sample(kernel.all.uptime[count:10])'

echo;echo "== Verify nth_percentile() functions for a non-singular metric"
echo Original with no function:
pmseries $args 'kernel.all.load[count:5]'
echo Compared to nth_percentile_inst:
pmseries $args 'nth_percentile_inst(kernel.all.load[count:5], 50)'
pmseries $args 'nth_percentile_inst(kernel.all.load[count:5])'
pmseries $args 'nth_percentile_inst(kernel.all.load[count:5], 500)'
echo Compared to nth_percentile_sample:
pmseries $args 'nth_percentile_sample(kernel.all.load[count:5], 50)'
pmseries $args 'nth_percentile_sample(kernel.all.load[count:5])'
pmseries $args 'nth_percentile_sample(kernel.all.load[count:5], 500)'

echo;echo "== Verify nth_percentile() functions for a non-singular metric, only one instance"
echo Original with no function:
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo Compared to nth_percentile_inst:
pmseries $args 'nth_percentile_inst(kernel.all.load{instance.name == "5 minute"}[count:5], 50)'
pmseries $args 'nth_percentile_inst(kernel.all.load{instance.name == "5 minute"}[count:5])'
pmseries $args 'nth_percentile_inst(kernel.all.load{instance.name == "5 minute"}[count:5], 500)'
echo Compared to nth_percentile_sample:
pmseries $args 'nth_percentile_sample(kernel.all.load{instance.name == "5 minute"}[count:5], 50)'
pmseries $args 'nth_percentile_sample(kernel.all.load{instance.name == "5 minute"}[count:5])'
pmseries $args 'nth_percentile_sample(kernel.all.load{instance.name == "5 minute"}[count:5], 500)'

echo;echo "== Verify nth_percentile() functions for a singular metric"
echo Original with no function:
pmseries $args 'kernel.all.uptime[count:10]'
echo Compared to nth_percentile_inst:
pmseries $args 'nth_percentile_inst(kernel.all.uptime[count:10], 50)'
pmseries $args 'nth_percentile_inst(kernel.all.uptime[count:10])'
pmseries $args 'nth_percentile_inst(kernel.all.uptime[count:10], 500)'
echo Compared to nth_percentile_sample:
pmseries $args 'nth_percentile_sample(kernel.all.uptime[count:10], 50)'
pmseries $args 'nth_percentile_sample(kernel.all.uptime[count:10])'
pmseries $args 'nth_percentile_sample(kernel.all.uptime[count:10], 500)'

echo;echo "== Verify topk() functions for a non-singular metric"
echo Original with no function:
pmseries $args 'kernel.all.load[count:5]'
echo Compared to topk_inst:
pmseries $args 'topk_inst(kernel.all.load[count:5],3)'
echo Compared to topk_sample:
pmseries $args 'topk_sample(kernel.all.load[count:5],3)'

echo;echo "== Verify topk() functions for a non-singular metric, only one instance"
echo Original with no function:
pmseries $args 'kernel.all.load{instance.name == "5 minute"}[count:5]'
echo Compared to topk_inst:
pmseries $args 'topk_inst(kernel.all.load{instance.name == "5 minute"}[count:5], 3)'
echo Compared to topk_sample:
pmseries $args 'topk_sample(kernel.all.load{instance.name == "5 minute"}[count:5], 3)'

echo;echo "== Verify topk() functions for a singular metric"
echo Original with no function:
pmseries $args 'kernel.all.uptime[count:10]'
echo Compared to topk_inst:
pmseries $args 'topk_inst(kernel.all.uptime[count:10], 3)'
echo Compared to topk_sample:
pmseries $args 'topk_sample(kernel.all.uptime[count:10], 3)'

# success, all done
status=0
exit
